package com.jy.adscm.hcPlan.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hcPlan.bean.HcPlanMakePart;
import com.jy.adscm.hcPlan.bean.HcPlanMakePartExample;
import com.jy.adscm.hcPlan.service.HcPlanMachineService;
import com.jy.adscm.hcPlan.service.HcPlanMakePartExampleService;
import com.jy.adscm.hcPlan.service.HcPlanMakePartService;
import com.jy.adscm.support.SimpleCoreResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 零件制造标准配置 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2018-08-13
 */
@RestController
@RequestMapping("/adscm/hcplan/hcPlanMakePart")
public class HcPlanMakePartController {


    @Autowired
    HcPlanMakePartExampleService hcPlanMakePartExampleService;
    @Autowired
    HcPlanMakePartService hcPlanMakePartService;
    @Autowired
    HcPlanMachineService hcPlanMachineService;


    /**
     * 获取所有零件
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getAllPart")
    public SimpleCoreResponse getAllPart() throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",hcPlanMakePartService.selectList(null));
    }
    /**
     * 增加零件标准配置
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/addPartConfig")
    public SimpleCoreResponse addPartConfig(HcPlanMakePart part) throws AdscmSystemException {

        return new SimpleCoreResponse("200","增加配置成功",hcPlanMakePartService.insert(part));
    }
    /**
     * 修改零件标准配置
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/editPartConfig")
    public SimpleCoreResponse editPartConfig(HcPlanMakePart part) throws AdscmSystemException {

        return new SimpleCoreResponse("200","修改配置成功",hcPlanMakePartService.updateById(part));
    }
    /**
     * 删除零件标准配置
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/delPartConfig/{id}")
    public SimpleCoreResponse delPartConfig(@PathVariable Integer id) throws AdscmSystemException {

        return new SimpleCoreResponse("200","删除配置成功",hcPlanMakePartService.deleteById(id));
    }

    /**
     * 获取所有总装批号对应的零件
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getAllMakePartExample")
    public SimpleCoreResponse getAllMakePartExample() throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",hcPlanMakePartExampleService.getAllMakePartExample());
    }
    /**
     * 获取所有机器
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getAllMachine")
    public SimpleCoreResponse getAllMachine() throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",hcPlanMachineService.selectList(null));
    }
    /**
     * 根据零件，获取制造零件的机器
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getPartMachine/{partId}")
    public SimpleCoreResponse getPartMachine(@PathVariable Integer partId) throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",hcPlanMakePartService.selectById(partId));
    }
    /**
     * 获取所有生产二部的员工
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getAllMakeUser")
    public SimpleCoreResponse getAllMakeUser() throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",hcPlanMakePartExampleService.getAllMakeUser());
    }

    /**
     * 程序自动计算，选择的一批待并单的零件的 生产时间
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/calculateMakePartTime")
    public SimpleCoreResponse calculateMakePartTime(@RequestParam(value = "ids[]") Long[] ids) throws AdscmSystemException {

//        return new SimpleCoreResponse("200","计算选择的零件生产时间成功",hcPlanMakePartExampleService.calculateMakePartTime(ids));
        return new SimpleCoreResponse("200","计算选择的零件生产时间成功",hcPlanMakePartExampleService.calculateMakePartTime_1(ids));
    }


    /**
     * 零件制造并单
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/mergePart/{start_date}/{end_date}")
    public SimpleCoreResponse mergePart(@PathVariable String start_date,@PathVariable String end_date,@RequestParam(value = "ids[]") Long[] ids,@RequestParam(value = "macCode")String macCode,@RequestParam(value = "operator")String operator,@RequestParam(value = "isUpdate")String isUpdate) throws AdscmSystemException {

        String mergeCode = hcPlanMakePartExampleService.mergePart(start_date,end_date,macCode,operator, Arrays.asList(ids),isUpdate);
        return new SimpleCoreResponse("200","零件制造并单成功",mergeCode);
    }
    /**
     * 零件制造，根据并单号，获取并单详情
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getMergePartDetail/{mergeId}")
    public SimpleCoreResponse getMergePartDetail(@PathVariable String mergeId) throws AdscmSystemException {

        return new SimpleCoreResponse("200","获取并单详情成功",hcPlanMakePartExampleService.getMergePartDetail(mergeId));
    }






    /**
     * 根据零件制造机器，获取每个机器下需要生产的零件和对应的批号信息
     * @param date ：日期，获取指定日期的，格式为：yyyy-MM，例如：2018-09，就是获取2018年9月份的
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getMachineHcAssembly/{date}")
    public SimpleCoreResponse getMachineHcAssembly(@PathVariable String date) throws AdscmSystemException {

        Map<String, List<HcPlanMakePartExample>> allHcPartExamples = hcPlanMakePartExampleService.getHcPartExamples(date);
        return new SimpleCoreResponse("200","查询成功",allHcPartExamples);
    }

    /**
     * 修改零件的制造机器
     * @param example
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/editMachineHcAssembly")
    public SimpleCoreResponse editMachineHcAssembly(HcPlanMakePartExample example) throws AdscmSystemException {

        hcPlanMakePartExampleService.editAllHcPartExamples(example);
        return new SimpleCoreResponse("200","查询成功",null);
    }
    /**
     * 装配单批次零件的制造数据
     * @param assembly_id
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/loadMachineHcAssembly/{assembly_id}")
    public SimpleCoreResponse loadMachineHcAssembly(@PathVariable Long assembly_id) throws AdscmSystemException {

        hcPlanMakePartExampleService.loadHcPartExampleByAssemblyId_new1(assembly_id);
        return new SimpleCoreResponse("200","装配成功",null);
    }

    /**
     * 提交零件制造任务
     * @param merge_id ：并单号
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/submitMakePartTask/{merge_id}")
    public SimpleCoreResponse submitMakePartTask(@PathVariable String merge_id) throws AdscmSystemException{

        hcPlanMakePartExampleService.submitMakePartTask(merge_id);

        return new SimpleCoreResponse("200","提交任务成功",null);

    }
}

